<?php
namespace app\store\controller\plugins\shop;
use app\store\controller\Controller;
use app\store\model\Shop as ShopModel;
use app\store\model\ShopClerk as ShopClerkModel;

/**
 * 店员管理控制器
 */
class Clerk extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
		$model = new ShopModel;
        $catgory = $model->getList();
        $model = new ShopClerkModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list','catgory'));
    }

    /**
     * 添加
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            $model = new ShopModel;
			$catgory = $model->getList();
            return $this->fetch('add', compact('catgory'));
        }
        $model = new ShopClerkModel;
        if ($model->add($this->postData('clerk'))) {
            return $this->renderSuccess('添加成功', url('plugins.shop.clerk/index'));
        }
        $error = $model->getError() ?: '添加失败';
        return $this->renderError($error);
    }

    /**
     * 删除
     */
    public function delete($shop_clerk_id)
    {
        $model = ShopClerkModel::get($shop_clerk_id);
        if (!$model->remove($shop_clerk_id)) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 编辑
     */
    public function edit($shop_clerk_id)
    {
        // 详情
        $model = ShopClerkModel::detail($shop_clerk_id);
        if (!$this->request->isAjax()) {
            $shop = new ShopModel;
			$catgory = $shop->getList();
            return $this->fetch('edit', compact('model', 'catgory'));
        }
        // 更新记录
        if ($model->edit($this->postData('clerk'))) {
            return $this->renderSuccess('更新成功', url('plugins.shop.clerk/index'));
        }
        $error = $model->getError() ?: '更新失败';
        return $this->renderError($error);
    }	
}
